package webserver.bank;

import java.io.IOException;
import java.net.UnknownHostException;

import std.bank.Account;

public class AccountRPC implements Account
{
	String className = "std.bank.AccountImpl";
	ClientStub client;

	public AccountRPC(int objectref)
	{
		client = new ClientStub(objectref);
	}

	public AccountRPC(String name, double balance)
	{
		Object[] parameterValues = { name, balance };
		Class<?>[] parameterTypes = { name.getClass(), double.class };
		try
		{
			client = new ClientStub(className, parameterTypes, parameterValues);
		}
		catch (UnknownHostException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public synchronized void deposit(double amount)
	{
		String methodName = "deposit";
		Object[] parameterValues = { amount };
		Class<?>[] paramaterTypes = { double.class };
		client.performMethodCall(className, methodName, paramaterTypes,
				parameterValues);
	}

	@Override
	public double getBalance()
	{
		String methodName = "getBalance";
		Object[] parameterValues = {};
		Class<?>[] paramaterTypes = {};
		String balanceString = (String) client.performMethodCall(className,
				methodName, paramaterTypes, parameterValues);
		Double balanceDouble = new Double(balanceString);
		return balanceDouble;
	}

	@Override
	public String getName()
	{
		String methodName = "getName";
		Object[] parameterValues = {};
		Class<?>[] paramaterTypes = {};
		return (String) client.performMethodCall(className, methodName,
				paramaterTypes, parameterValues);
	}

	@Override
	public String toString()
	{
		String methodName = "toString";
		Object[] parameterValues = {};
		Class<?>[] paramaterTypes = {};
		return (String) client.performMethodCall(className, methodName,
				paramaterTypes, parameterValues);
	}

	@Override
	public synchronized void withdraw(double amount)
	{
		if (amount > 0)
		{
			String methodName = "withdraw";
			Object[] parameterValues = { amount };
			Class<?>[] paramaterTypes = { double.class };
			client.performMethodCall(className, methodName, paramaterTypes,
					parameterValues);
		}
	}

}
